package com.easyOrder.core.push;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

public class IOSPushClient {
	
	@Autowired
	private IosDelivery iosDelivery;

	@Autowired
	private IosDelivery iosDelivery2;

	public IOSPushClient() {
	}

	public void send(Notification message) {
		ArrayList<Recipient> iosRecipients = null;
		ArrayList<Recipient> iosRecipients2 = null;
		ArrayList<Recipient> androidRecipients = null;
		for (int i = 0; i < message.getRecipients().size(); i++) {
			Recipient recipient = message.getRecipients().get(i);
			if (recipient.getIos()) {
				if (2 == recipient.getApplication()) {
					if (iosRecipients2 == null) {
						iosRecipients2 = new ArrayList<Recipient>();
					}
					iosRecipients2.add(recipient);
				} else {
					if (iosRecipients == null) {
						iosRecipients = new ArrayList<Recipient>();
					}
					iosRecipients.add(recipient);
				}
			} else {
				if (androidRecipients == null) {
					androidRecipients = new ArrayList<Recipient>();
				}
				androidRecipients.add(recipient);
			}
		}
		if (iosRecipients != null) {
			message.setRecipients(iosRecipients);
			iosDelivery.send(message);
		}
		if (iosRecipients2 != null) {
			message.setRecipients(iosRecipients2);
			iosDelivery2.send(message);
		}
	}
}
